package com.hashwolf.newpipe.util;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;

import com.hashwolf.newpipe.R;

import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;

public class ThemeHelper {
    private ThemeHelper() {
    }


    public static void setTheme(final Context context) {

    }

    public static void setTheme(final Context context, final int serviceId) {
        context.setTheme(getThemeForService(context, serviceId));
    }

    public static void setDayNightMode(final Context context) {
        setDayNightMode(context, ThemeHelper.getSelectedThemeKey(context));
    }

    public static void setDayNightMode(final Context context, final String selectedThemeKey) {
        final Resources res = context.getResources();

        if (selectedThemeKey.equals(R.string.light_theme_key)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        } else if (selectedThemeKey.equals(R.string.dark_theme_key) || selectedThemeKey.equals(R.string.black_theme_key)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        }
    }

    /**
     * 根据serviceId返回主题样式
     *
     * @param context   上下文用于获取相关主题配置
     * @param serviceId 用于获取相关主题，-1则返回默认主题
     * @return 与serviceId关联的style
     */
    public static int getThemeForService(final Context context, final int serviceId) {
        final Resources res = context.getResources();
        final String lightThemeKey = res.getString(R.string.light_theme_key);
        final String blackThemeKey = res.getString(R.string.black_theme_key);
        final String automaticDeviceThemeKey = res.getString(R.string.auto_device_theme_key);

        final String selectedThemeKey = getSelectedThemeKey(context);

        // 从应用上下文获取主题
        int baseTheme = R.style.DarkTheme;
        if (selectedThemeKey.equals(lightThemeKey)) {
            baseTheme = R.style.LightTheme;
        } else if (selectedThemeKey.equals(blackThemeKey)) {
            baseTheme = R.style.BlackTheme;
        } else if (selectedThemeKey.equals(automaticDeviceThemeKey)) {
            if (isDeviceDarkThemeEnabled(context)) {
                final String selectedNightThemeKey = getSelectedNightThemeKey(context);
                if (selectedNightThemeKey.equals(blackThemeKey)) {
                    baseTheme = R.style.BlackTheme;
                } else {
                    baseTheme = R.style.DarkTheme;
                }
            } else {
                // 设置主题是系统主题且非深色主题则返回浅色主题
                return R.style.LightTheme;
            }
        }
        // serviceId <= 1 返回默认主题
        if (serviceId <= -1) {
            return baseTheme;
        }

        // 从NewPipe extractor里获取主题
        final StreamingService service;
        try {
            service = NewPipe.getService(serviceId);
        } catch (final ExtractionException ignored) {
            return baseTheme;
        }

        String themeName = "DarkTheme"; // default
        if (baseTheme == R.style.LightTheme) {
            themeName = "LightTheme";
        } else if (baseTheme == R.style.BlackTheme) {
            themeName = "BlackTheme";
        }

        themeName += "." + service.getServiceInfo().getName();
        // TODO: 后续替换getIdentifier方法
        final int resourceId = context.getResources().getIdentifier(themeName, "style", context.getPackageName());

        if (resourceId > 0) {
            return resourceId;
        }
        return baseTheme;
    }

    /**
     * 获取设备主题
     *
     * @param context app应用上下文
     * @return true为深色主题 false为浅色主题或者未知
     */
    public static boolean isDeviceDarkThemeEnabled(final Context context) {
        final int deviceTheme = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        switch (deviceTheme) {
            case Configuration.UI_MODE_NIGHT_YES:
                return true;
            case Configuration.UI_MODE_NIGHT_UNDEFINED:
            case Configuration.UI_MODE_NIGHT_NO:
            default:
                return false;
        }
    }

    private static String getSelectedThemeKey(final Context context) {
        final Resources res = context.getResources();
        final String themeKey = res.getString(R.string.theme_key);
        final String defaultTheme = res.getString(R.string.default_theme_value);
        return PreferenceManager.getDefaultSharedPreferences(context).getString(themeKey, defaultTheme);
    }

    private static String getSelectedNightThemeKey(final Context context) {
        final String nightThemeKey = context.getString(R.string.night_theme_key);
        final String defaultNightThemeKey = context.getString(R.string.dark_theme_key);
        return PreferenceManager.getDefaultSharedPreferences(context).getString(nightThemeKey, defaultNightThemeKey);
    }
}
